<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * @version 1.0
 * @author xpl0it
*/

class User extends CI_Controller {
	public $table = "admin_users";
     
	function __construct() {
	 	parent::__construct();
	 	$this->load->model('admin_model','model');
        $this->output->enable_profiler(TRUE);
	 }

	public function index(){
		$this->checkauth();
		$pon = $this->model;
		$data['query']= $pon->select_by_table($this->table);
		$content = $this->load->view('admin/detail', $data,true);
		$content_data = new stdClass(); 
		$content_data->content = $content;
		$this->load->view('template/render_dt',$content_data);
	}

	function add($data=array()){ 
		$this->checkauth();
		$data="";
		$content = $this->load->view('admin/form', $data,true);
		$content_data = new stdClass(); 
		$content_data->content = $content;
		$this->load->view('template/render',$content_data);
	}
    
    function update($param){
        $this->checkauth();
        $data['val']=$this->model->select_by_filter($param);
		$content = $this->load->view('admin/form', $data,true);
		$content_data = new stdClass(); 
		$content_data->content = $content;
		$this->load->view('template/render',$content_data);
                    
    }
                
    function save(){
        $this->checkauth();
        if($_POST['admin_id'] !=  ''){
        	$rs = $this->model->update_data($this->input->post(NULL,TRUE));
				if($rs){
					$this->load->view('template/header');
					$data = '<script type="text/javascript">alert("แก้ไขข้อมูลสำเร็จแล้ว !");location = "'.base_url("index.php/admin/user").'";</script>';
					echo $data;
				} else {
					$this->load->view('template/header');
					$data = '<script type="text/javascript">alert("ข้อมูลไม่ถูกต้อง ไม่สามารถแก้ไขข้อมูลได้ !");window.history.back();</script>';
					echo $data;
				}
	    } else {
	    $rs = $this->model->insert_data($this->input->post(NULL,TRUE));
	            if($rs){
					$this->load->view('template/header');
					$data = '<script type="text/javascript">alert("บันทึกข้อมูลสำเร็จแล้ว !");location = "'.base_url("index.php/admin/user").'";</script>';
					echo $data;
				} else {
					$this->load->view('template/header');
					$data = '<script type="text/javascript">alert("ข้อมูลไม่ถูกต้อง ไม่สามารถบันทึกข้อมูลได้ !");window.history.back();</script>';
					echo $data;
				}
         }
                    
    }

    function del($id){
		$this->checkauth();
		$rs = $this->model->delete_data($id);
		if($rs){
			$this->load->view('template/header');
			$data = '<script type="text/javascript">alert("ลบข้อมูล้สำเร็จแล้ว ! ");location = "'.base_url("index.php/admin/user").'";</script>';
			echo $data;
		}
	}














	
	private function checkauth(){
		if(!$this->session->userdata('authen')){
			redirect('admin/login/','refresh');
		}
	}

}